package com.yadang.redisson;

import com.yadang.redisson.client.RedissonClient;
import org.redisson.Redisson;

import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author: 亚当
 * @date: 2023年02月12日 12:11
 * @package: com.yadang.redisson
 * @Description: TODO
 */
public class RedisLockTest {
    public static void main(String[] args) {
        RedissonClient redissonClient = new RedissonClient();
        Redisson redisson = redissonClient.getRedisson();
        RedissonLock redissonLock = new RedissonLock(redisson,"lockName");
        // 定义共享资源
        AtomicInteger sharedResource = new AtomicInteger();

        // 创建多个线程模拟并发访问共享资源
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                boolean acquire = redissonLock.acquire(1, 3);
                try {
                    // 访问共享资源
                    sharedResource.getAndIncrement();
                    System.out.println("Shared resource value: " + sharedResource);
                } finally {
                    redissonLock.release();
                }
            }).start();
        }

        // 关闭Redisson客户端
        redissonClient.close();
    }
}
